<?php
/***
 * 描述：团购商品管理
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: TgProdManageAction.class.php 3 2013-10-14 02:49:41Z whtaok@gmail.com $
 */
class TgProdManageAction extends AdminAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="TgProdLib";
	}
	
	/**
	 * 团购商品基本库查询
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'barcode'=>I('barcode'),
		);
		$mod=new TgProdLibModel();
		list($list,$page) = $mod->findListPage($search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('index');
	}
	
	/**
	 * 显示添加页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showAddAct($add_cnt=10) {
		$this->assign('cateTree',productcate_tree());
		$this->display('add');
	}
	
	/**
	 * 添加操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddAct() {
		$cate_code2  = I('cate_code');
		$cate_code1 = get_productcate_code1($cate_code2);
		$mod = new TgProdLibModel();
		if($mod->create()){
			$mod->cate_code1 = $cate_code1;
			$mod->cate_code2 = $cate_code2;
			$lastId = $mod->add();
			if($lastId){
				$this->success('添加成功！','index');
			}else{
				$this->error('添加失败！');
			}
		}else{
			$this->error($mod->getError());
		}

		
	}
	
	/**
	 * 显示编辑页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdAct() {
		$mod=new TgProdLibModel();
		$detail = $mod->find(I('id'));
		if(I('upd_type')=='mobile'||I('upd_type')=='pc'){
			$detail['upd_type'] = I('upd_type');
			$this->assign("detail",$detail);
			$this->assign('cateTree',productcate_tree());
			$this->display('upd_content');
		}else{
			$this->assign("detail",$detail);
			$this->assign('cateTree',productcate_tree());
			$this->display('upd');
		}
		
	}
	
	/**
	 * 更新编辑页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdAct() {
		$mod=new TgProdLibModel();
		if(I('upd_type')=='mobile'||I('upd_type')=='pc'){
			//更新详细
		}else{
			$cate_code2  = I('cate_code');
			$cate_code1 = get_productcate_code1($cate_code2);
			
		}
		
		
		if($mod->create()){
			if(I('upd_type')=='mobile'||I('upd_type')=='pc'){
				//更新详细		
			}else{
				$mod->cate_code1 = $cate_code1;
				$mod->cate_code2 = $cate_code2;	
			}
			$flag = $mod->save();
			if($flag !== false){
				$this->success('修改成功！');
			}else{
				$this->success('修改时发生错误，请重新尝试！');
			}
		}else{
			$this->error($mod->getError());
		}
	}	
	
	/**
	 * 删除操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct(){
		$id_list = array_values(I('id'));
		if($id_list){
			$mod = new TgProdLibModel();
			$data['id'] = array('in',$id_list);
			$mod->where($data)->delete();
		}
		$this->success('删除成功', U('index'));
	}
	
	/**
	 * 描述：上传图片
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-4-23
	 */
	public function doUploadAct() {
		$id = I('id');
		//访问路径 http：//
		$pathUrl = photo_url_path('/tg_prod/');
		//上传路径 file：///
		$pathReal = photo_real_path('/tg_prod/',true);
		$myconfig = array(
				'allowExts'         =>  array('jpg'),   // 允许上传的文件后缀 留空不作后缀检查
				'saveRule'          =>  $id,			// 上传文件命名规则
				'uploadReplace'     =>  true,// 存在同名是否覆盖
				'thumbMaxWidth'     =>  '160',// 缩略图最大宽度
				'thumbMaxHeight'    =>  '160',// 缩略图最大高度
			);
		$upload = $this->doUpload($pathReal,$myconfig);
		if($upload->getErrorMsg()){
			$rtn_data = array('error'=>0,'message'=>$upload->getErrorMsg());
		}else{
			$uploadFiles = $upload->getUploadFileInfo();
			$rtn_data = array(
					'error'=>0,
					'id'=>$id,
					'url'=>$pathUrl.$id.'.png'
			);
	
		}
		header('Content-Type:text/html; charset=utf-8');
		//header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($rtn_data));
	}


}

?>